package com.kk.permission.huawei.actionutil;

import android.accessibilityservice.AccessibilityService;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kk.R;
import com.kk.permission.accessibilityutils.NodeUtil;
import com.kk.permission.huawei.HuaweiActionUtil;
import com.kk.permission.huawei.HuaweiTool;

import java.util.HashMap;
import java.util.List;

public class HuaweiCallNotificationUtil {
    private final String STEP1 = "step1";
    private final String STEP2 = "step2";
    private final String STEP3 = "step3";
    private final String STEP4 = "step4";
    private final String STEP5 = "step5";
    private final String STEP6 = "step6";
    private String TAG = "CallNotification";
    private String mAppName = HuaweiTool.getAppName();
    private HashMap<String, String> mStepMap = new HashMap();
    private boolean result = false;

    public boolean callNotification(AccessibilityNodeInfo accessibilityNodeInfo, AccessibilityService accessibilityService) {
        if (this.result) {
            return true;
        }
        if (NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getString(R.string.accessiblity_permission_huawei_dial_noti_title))) {
            if (!this.mStepMap.containsKey("step2") || this.mStepMap.containsKey("step3")) {
                List findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(this.mAppName);
                if (findAccessibilityNodeInfosByText == null || findAccessibilityNodeInfosByText.size() <= 0) {
                    if (!this.mStepMap.containsKey("step1")) {
                        HuaweiActionUtil.scrollForward(accessibilityNodeInfo);
                    }
                } else if (!this.mStepMap.containsKey("step1")) {
                    if(HuaweiActionUtil.performSwitch(accessibilityNodeInfo, this.mAppName)){
                        this.mStepMap.put("step1", "1");
                    }else {
                        HuaweiTool.actionDialNotiDone();
                        NodeUtil.back(accessibilityService, 500);
                    }
//                    this.mStepMap.put("step1", "1");
//                    HuaweiActionUtil.performSwitch(accessibilityNodeInfo, this.mAppName);
//                    HuaweiTool.actionDialNotiDone();
                }
            } else {
                this.mStepMap.put("step3", "1");
                this.result = true;
                HuaweiTool.actionDialNotiDone();
                NodeUtil.back(accessibilityService, 500);
            }
        } else if (NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getString(R.string.accessiblity_permission_huawei_dial_noti)) && NodeUtil.pageContains(accessibilityNodeInfo, this.mAppName)) {
            String string = HuaweiTool.getString(R.string.accessiblity_permission_huawei_doze_all_ok);
            String string2 = HuaweiTool.getString(R.string.accessibility_permission_miuiv6_allowed);
            if (this.mStepMap.containsKey("step1") && !this.mStepMap.containsKey("step2")) {
                this.mStepMap.put("step2", "1");
                NodeUtil.clickByText(accessibilityNodeInfo, string);
                NodeUtil.clickByText(accessibilityNodeInfo, string2);
            }
        } else if (NodeUtil.pageContains(accessibilityNodeInfo, "停用") && NodeUtil.pageContains(accessibilityNodeInfo, "取消")) {
            if (this.mStepMap.containsKey("step1") && !this.mStepMap.containsKey("step2")) {
                this.mStepMap.put("step2", "1");
                NodeUtil.clickByText(accessibilityNodeInfo, "取消");
                NodeUtil.back(accessibilityService, 500);
            }
        }
        return false;
    }
}
